<script setup lang="ts">
import {
  deleteSalesOutStoreApi,
  getSalesOutStoreListApi,
} from "@/api/SalesOutStore";
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { SalesOutStoreResultsData } from "@/api/SalesOutStore/res";
import {
  SalesOutStoreParamsType,
  PaginationType,
  SalesOutStoreSearchType,
} from "@/api/SalesOutStore/params";

// mockData
let sourceData = ref(["销售订单", "库存备货", "其他"]);
let stateData = ref(["未分解", "已分解", "已完成", "已关闭", "已撤回"]);
const filteredSearchData: Partial<SalesOutStoreSearchType> = {};
let searchData = ref<SalesOutStoreSearchType>({
  outStoreListId: "",
  shippingCheckList: "",
  outStoreDate: "",
  salesListId: "",
  produceDate: "",
  endDate: "",
});
let tableData = ref<SalesOutStoreResultsData[]>([
  {
    shippingCheckList: "",
    salesListId: "",
    outStoreDate: "",
    storeLocationName: "",
    storeAreaName: "",
    unit: "",
    outStoreListId: "",
    outStoreListName: "",
    customerName: "",
    produceDate: "",
    productName: "",
    salesListName: "",
    productId: "",
    outStoreNum: "",
    endDate: "",
    leader: "",
    specs: "",
    batchId: "",
    storeName: "",
    objectId: "",
  },
]);
let total = ref(0);
let showDatePicker = ref(false);
const $router = useRouter();
let loading = ref(false);
let ifSearch = ref(false);
let params = ref<PaginationType>({
  currentPage: 1,
  pageSize: 10,
});

const handler = (page) => {
  params.value.currentPage = page;
  if (ifSearch.value) {
    getTableData(filteredSearchData);
  } else {
    getTableData();
  }
};
const sizeHandler = (page_size) => {
  params.value.pageSize = page_size;
  if (ifSearch.value) {
    getTableData(filteredSearchData);
  } else {
    getTableData();
  }
};
const gotoAdd = () => {
  $router.push("/storage/entrance/addSale");
};
const gotoDetail = () => {
  $router.push("/storage/entrance/saleDetail");
};

const updateHandler = (objectId: string) => {
  $router.push({
    path: "/storage/entrance/updateSale",
    query: { objectId },
  });
};

const handleClick = () => {};

const getTableData = async (data?) => {
  try {
    loading.value = true;
    const TotalRes = await getSalesOutStoreListApi();
    total.value = TotalRes.results.length;
    const res = await getSalesOutStoreListApi(data, params.value);
    tableData.value = res.results;
    if (ifSearch.value) {
      total.value = res.results.length;
    }
  } catch (err: any) {
    console.log(err);
    ElMessage.error("获取数据失败");
  } finally {
    loading.value = false;
  }
};

const deleteData = async (objectId: string) => {
  try {
    await deleteSalesOutStoreApi(objectId);
    getTableData();
    ElMessage.success("删除成功");
  } catch (err: any) {
    console.log(err);
    ElMessage.error("删除失败");
  }
};

const onSearch = () => {
  let allFieldsEmpty = true;
  Object.keys(filteredSearchData).forEach((key) => {
    delete filteredSearchData[key];
  });
  for (const key in searchData.value) {
    if (searchData.value[key] !== "" && searchData.value[key] !== null) {
      allFieldsEmpty = false;
      filteredSearchData[key] = searchData.value[key];
    }
  }
  ifSearch.value = !allFieldsEmpty;
  params.value.currentPage = 1;
  params.value.pageSize = 10;
  getTableData(filteredSearchData);
};
const reset = () => {
  searchData.value = {
    outStoreListId: "",
    shippingCheckList: "",
    outStoreDate: "",
    salesListId: "",
    produceDate: "",
    endDate: "",
  };
  params.value.currentPage = 1;
  params.value.pageSize = 10;
  ifSearch.value = false;
  getTableData();
};

onMounted(async () => {
  getTableData();
});
</script>

<template>
  <div style="padding: 20px">
    <el-card>
      <div class="operateContainer">
        <div
          class="formContainer"
          :style="showDatePicker ? 'height:85px' : 'height:32px'"
        >
          <el-form inline :model="searchData">
            <el-form-item prop="outStoreListId">
              <el-input
                placeholder="请输入出库单号/名称"
                style="width: 250px"
                v-model.trim="searchData.outStoreListId"
              ></el-input>
            </el-form-item>
            <el-form-item prop="shippingCheckList">
              <el-input
                placeholder="请输入出货检验单"
                style="width: 250px"
                v-model.trim="searchData.shippingCheckList"
              ></el-input>
            </el-form-item>
            <el-form-item prop="outStoreDate">
              <el-date-picker
                style="width: 250px"
                type="date"
                placeholder="请选择出库时间"
                v-model.trim="searchData.outStoreDate"
              ></el-date-picker>
            </el-form-item>
            <div style="margin-top: 20px">
              <el-form-item prop="salesListId">
                <el-input
                  placeholder="请输入销售单号/名称"
                  style="width: 250px"
                  v-model.trim="searchData.salesListId"
                ></el-input>
              </el-form-item>
              <el-form-item prop="produceDate">
                <el-date-picker
                  style="width: 250px"
                  type="date"
                  placeholder="请选择生产日期"
                  v-model.trim="searchData.produceDate"
                ></el-date-picker>
              </el-form-item>
              <el-form-item prop="endDate">
                <el-date-picker
                  style="width: 250px"
                  type="date"
                  placeholder="请选择到期日期"
                  v-model.trim="searchData.endDate"
                ></el-date-picker>
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div class="searchOperate">
          <div class="button">
            <el-button type="primary" plain style="width: 80px" @click="reset"
              >重置</el-button
            >
            <el-button type="primary" style="width: 80px" @click="onSearch"
              >查询</el-button
            >
          </div>
          <div class="more">
            <el-button
              type="primary"
              link
              @click="showDatePicker = !showDatePicker"
              >更多</el-button
            >
            <el-icon color="#1B9AEE" :class="{ 'rotate-180': showDatePicker }">
              <CaretBottom />
            </el-icon>
          </div>
        </div>
      </div>
    </el-card>
    <el-card style="margin-top: 20px">
      <template #header>
        <span style="font-weight: bold">销售出库</span>
      </template>
      <div class="operate">
        <el-button style="width: 80px" type="primary" @click="gotoAdd"
          >新增</el-button
        >
        <el-button style="width: 80px" type="primary" plain>导出</el-button>
      </div>
      <el-table
        :data="tableData"
        style="margin-top: 15px"
        border
        v-loading="loading"
      >
        <el-table-column
          align="center"
          label="序号"
          type="index"
          width="60"
          fixed="left"
        ></el-table-column>
        <el-table-column
          align="center"
          label="出库单名称"
          fixed="left"
          width="100"
          prop="outStoreListName"
        >
          <template #="{ row, $index }">
            <el-button link type="primary" @click="gotoDetail">{{
              row.outStoreListName
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="出库单编号"
          fixed="left"
          width="110"
          prop="outStoreListId"
        ></el-table-column>
        <el-table-column
          align="center"
          label="出货检验单"
          width="110"
          prop="shippingCheckList"
        ></el-table-column>
        <el-table-column
          align="center"
          label="出库日期"
          width="100"
          prop="outStoreDate"
        ></el-table-column>
        <el-table-column
          align="center"
          label="销售单号"
          width="100"
          prop="salesListId"
        ></el-table-column>
        <el-table-column
          align="center"
          label="销售单名称"
          width="100"
          prop="salesListName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="客户名称"
          width="100"
          prop="customerName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="产品名称"
          width="90"
          prop="productName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="产品编号"
          width="100"
          prop="productId"
        ></el-table-column>
        <el-table-column
          align="center"
          label="规格型号"
          width="90"
          prop="specs"
        ></el-table-column>
        <el-table-column
          align="center"
          label="单位"
          prop="unit"
        ></el-table-column>
        <el-table-column
          align="center"
          label="生产日期"
          width="100"
          prop="produceDate"
        ></el-table-column>
        <el-table-column
          align="center"
          label="到期日期"
          width="100"
          prop="endDate"
        ></el-table-column>
        <el-table-column
          align="center"
          label="批次号"
          prop="batchId"
          width="110"
        ></el-table-column>
        <el-table-column
          align="center"
          label="出库数量"
          width="90"
          prop="outStoreNum"
        ></el-table-column>
        <el-table-column
          align="center"
          label="仓库名称"
          width="90"
          prop="storeName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="库区名称"
          width="90"
          prop="storeAreaName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="库位名称"
          width="90"
          prop="storeLocationName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="仓库负责人"
          width="100"
          prop="leader"
        ></el-table-column>
        <el-table-column align="center" label="操作" width="200" fixed="right">
          <template #="{ row, $index }">
            <el-button link type="primary" @click="handleClick">
              出库
            </el-button>
            <el-button link type="primary" @click="updateHandler(row.objectId)"
              >编辑</el-button
            >
            <el-button link type="primary" @click="deleteData(row.objectId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.currentPage"
        v-model:page-size="params.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout="prev,pager,next,jumper,->,sizes,total"
        :total="total"
        @current-change="handler"
        @size-change="sizeHandler"
        style="margin-top: 10px"
      ></el-pagination>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.operateContainer {
  display: flex;
  align-content: center;
  justify-content: space-between;
  .formContainer {
    width: 1060px;
    height: 32px;
    transition: height 0.3s ease; // 添加高度变化的过渡效果
    overflow: hidden; // 防止内容溢出
    .el-form-item {
      margin-bottom: 0;
    }
  }
  .searchOperate {
    display: flex;
    align-items: center;
    .more {
      margin-left: 20px;
      display: flex;
      align-items: center;
    }
  }
}

/* 定义旋转动画 */
.rotate-180 {
  transition: transform 0.3s ease;
  transform: rotate(180deg);
}
</style>
